package com.tensquare.article.controller;


import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
* Created by : 大萝卜
* Date: 2020-4-6        mongodb
*/

@Slf4j
@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Comment> list = this.commentService.findAll();
        if (CollectionUtils.isEmpty(list)){
            log.info("findAll方法，list为空：{}",list);
            return new Result(false, StatusCode.ERROR,"参数错误");
        }
        log.info("查询评论成功,{}",request.getLocalName());
        return new Result(true, StatusCode.OK,"查询评论成功",list);
    }


    @RequestMapping(value = "{commentId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("commentId") String commentId){
        Comment comment = this.commentService.findById(commentId);
        if (comment == null){
            log.info("findById方法，comment为空：{}",comment.toString());
            return new Result(false, StatusCode.ERROR,"参数错误");
        }
        log.info("查询ID成功,{}",request.getLocalName());
        return new Result(true, StatusCode.OK,"查询评论成功",comment);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Comment comment){
        log.info("进行新增操作！");
        this.commentService.save(comment);
        return new Result(true, StatusCode.OK,"新增成功");
    }

    @RequestMapping(value = {"{commentId}"},method = RequestMethod.PUT)
    public Result updateById(@PathVariable String commentId,
                             @RequestBody Comment comment){
        log.info("进行修改操作！");
        comment.set_id(commentId);
        this.commentService.updateById(comment);
        return new Result(true, StatusCode.OK,"修改成功");
    }

    @RequestMapping(value = "{commentId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String commentId){
        log.info("进行删除操作！");
        this.commentService.deleteById(commentId);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    /**
    * Created by : 大萝卜
    * Date: 2020-4-6        根据文章id查询评论数据
    */
    @RequestMapping(value = "article/{articleId}", method = RequestMethod.GET)
    public Result findByArticleId(@PathVariable String articleId){
        List<Comment> list = this.commentService.findByArticleId(articleId);
        if (CollectionUtils.isEmpty(list)){
            log.info("findById方法，list为空：{}",list);
            return new Result(false, StatusCode.ERROR,"参数错误");
        }
        log.info("查询评论成功,{}",list);
        return new Result(true,StatusCode.OK,"查询评论成功",list);
    }

    /**
    * Created by : 大萝卜
    * Date: 2020-4-6        文章点赞功能(根据评论的id，来对评论点赞)
    */
    @RequestMapping(value = "thumbup/{commentId}",method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String commentId){
        String userId = "USER_ID";
        Object flag = this.redisTemplate.opsForValue().get("THUMBUP_" + userId + "_" + commentId);
        if (flag == null){
            //没有点过赞
            this.commentService.thumbup(commentId);
            log.info("点赞成功+1");
            //redis记录点赞
            this.redisTemplate.opsForValue().set("THUMBUP_" + userId + "_" + commentId,1);
            return new Result(true,StatusCode.OK,"点赞成功");
        }
        this.commentService.thumbups(commentId);
        log.info("取消点赞-1");
        this.redisTemplate.delete("THUMBUP_" + userId + "_" + commentId);
        log.info("删除点赞缓存完成");
        return new Result(true,StatusCode.OK,"已取消点赞");
    }
}
